/*
 * @Author: your name
 * @Date: 2020-06-30 15:44:20
 * @LastEditTime: 2021-09-28 18:52:54
 * @LastEditors: wangwangwang
 * @Description: In User Settings Edit
 * @FilePath: \icps_baseWeb\vue.config.js
 */
const path = require('path')
const resolve = dir => path.join(__dirname, dir)
const isProduction = process.env.NODE_ENV === 'production';
const Happypack = require('happypack');
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg|png)(\?.*)?$/i;
// const externals = process.env.NODE_ENV == 'development' ? {} : {
//   axios: 'axios',
// }
/**图片压缩
 * cnpm install image-webpack-loader --save-dev
*/
/**
 * 多页面配置
 * */

 const IS_PROD = ['production', 'test'].includes(process.env.NODE_ENV)

module.exports = {
  publicPath: './',
  productionSourceMap: true,//是否打包源码最终使用false
  assetsDir: 'static',
  css: {
    requireModuleExtension: true,
    extract: IS_PROD,
    sourceMap: true,
    loaderOptions: {
      sass: {
        prependData: `@import "@/app.scss";`
      },
      less: {
        javascriptEnabled: true
      }
    }
  },
  configureWebpack: config => {
    // config.externals = externals
    config.devtool = 'source-map'
    config.plugins.push(
      new Happypack({
        loaders: ['babel-loader', 'vue-loader', 'url-loader'],
        cache: true,
        threads: 16
      })
    )
    if (isProduction) {
      config.plugins.push(
        new CompressionWebpackPlugin({
          filename: '[path].gz[query]',
          algorithm: 'gzip',
          test: productionGzipExtensions,
          threshold: 10240,
          minRatio: 0.8,
          deleteOriginalAssets: false,
        })
      )
    }
  },
  chainWebpack: config => {
    /**别名*/
    config.resolve.alias.set("@", resolve("src"));
    if (isProduction) {
      config.plugins.delete('prefetch');
      config.plugins.delete('preload');
      /** ============压缩图片 start============ */
      config.module
        .rule('images')
        .use('image-webpack-loader')
        .loader('image-webpack-loader')
        .options({ bypassOnDebug: true })
        .end()
      /**小于10K的图片内联*/
      const imagesRule = config.module.rule('images')
      imagesRule.uses.clear()
      imagesRule
        .test(/\.(png|jpe?g|gif|svg|webp)$/i)
        .use('file-loader')
        .loader('url-loader')
        .options({
          limit: 10240,
          fallback: {
            loader: 'file-loader',
            options: {
              outputPath: 'static/images'
            }
          }
        })
        .end()
      config.optimization.minimize(true)
    }

  },
  devServer: {
    port: 8080,
    open: true,
    disableHostCheck: true,
    proxy: {
      // // 方便本地联调开发
      '/dev/base': {
        // target: 'https://lvshe.huashijc.com/',
        target: 'http://10.31.5.66',
        // target: 'http://10.31.6.130:8070/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev/base': '/base',
        }
      },
      '/base': {
        // target: 'https://lvshe.huashijc.com/',
        target: 'http://10.31.5.66',
        // target: 'http://10.31.6.130:8070/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/base': '/base',
        }
      },
      // 文件上传验证
      '/dev/resource/': {
        target: 'http://10.31.5.66',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev/resource/': '/resource/',
        }
      },
      // 文件上传
      '/resource/resources': {
        target: 'http://10.31.5.66',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/resource/resources': '/resource/resources',
        }
      },
      // 图算导入
      '/icps/system/sign/basics/graphic/importList': {
        // target: 'https://lvshe.huashijc.com/',
        target: 'http://10.31.5.66',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/icps/system/sign/basics/graphic/importList': '/icps/system/sign/basics/graphic/importList',
        }
      },
      // 日志
      '/dev/logs': {
        // target: 'https://lvshe.huashijc.com/',
        target: 'http://10.31.5.66',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev/logs': '/logs',
        }
      },
      // 调度维护
      '/dev/schedule': {
        // target: 'https://lvshe.huashijc.com/',
        target: 'http://10.31.5.66',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev/schedule': '/schedule',
        }
      },
      // //对外接口
      '/dev/third': {
        // target: 'https://lvshe.huashijc.com/',
        target: 'http://10.31.5.66',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev/third': '/third'
        }
      },
      // 首页配置
      '/dev/analysis': {
        // target: 'https://lvshe.huashijc.com/',
        target: 'http://10.31.5.66',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev/analysis': '/analysis',
        }
      },
      // 首页数据
      '/dev/message': {
        // target: 'https://lvshe.huashijc.com/',
        target: 'http://10.31.5.66',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev/message': '/message',
        }
      },
      // 导入供应商
      '/supplier/importData': {
        // target: 'https://lvshe.huashijc.com/',
        target: 'http://10.31.5.66',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/supplier/importData': '/supplier/importData',
        }
      },
      // //积分的
      // '/dev/integral': {
      //   target: 'http://10.31.6.162:9002/',
      //   ws: true,
      //   changeOrigin: true,
      //   pathRewrite: {
      //     '^/dev/integral': '/integral'
      //   }
      // },
      // //签核的
      '/dev/workflow': {
        // target: 'https://lvshe.huashijc.com/',
        target: 'http://10.31.5.66',
        changeOrigin: true,
        pathRewrite: {
          '^/dev/workflow': '/workflow'
        }
      },
      // //积分的
      // '/dev/integral': {
      //   target: 'http://10.31.6.162:9002/',
      //   ws: true,
      //   changeOrigin: true,
      //   pathRewrite: {
      //     '^/dev/integral': '/integral'
      //   }
      // },
      // //签核的
      '/dev/integral': {
        // target: 'https://lvshe.huashijc.com/',
        target: 'http://10.31.5.66',
        changeOrigin: true,
        pathRewrite: {
          '^/dev/integral': '/integral'
        }
      },
      '/dev': {
        target: process.env.VUE_APP_API_HOST, // 这里写你自己的服务器域名
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev': '',
        }
      },
      '/file': {
        target: 'http://10.31.5.66',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/file': '/file'
        }
      },
    },
  },
}

